var zTreeMenu;
var menuRoot={ id:0, parent_id:null, group_name:"用户分组", open:true};
var ztreeSeting = {
			view: { showLine : false },
			callback:{
				//单击
				onClick:onClick
			},
			data: {
				key:{
					name:"group_name"//节点名称的属性名称
				},
				simpleData: {
					enable: true,
					idKey: "id",
					pIdKey: "parent_id",
					rootPId: null
				}
			}

};

var comboboxDatas;
//document ready
$(function(){
	$('#addButton').bind('click', addMenu);//添加按钮绑定事件
	$('#deleteButton').bind('click', deleteMenu);//添加按钮绑定事件
	$('#saveButton').bind('click', saveMenu);//保存按钮绑定事件
	
	initmenu();
	
	//=[{'id':'enabled','text':'启用'},{'id':'disable','text':'禁用'}]
	$.ajax({
		type: "post",
		contentType:"application/json",
		url: "/authenticated/grid/column/sys_user_group",
		data:null,
		dataType:"json",
		success: function(result){
			if(result.success){
				comboboxDatas=result.data;
				initComboboxDatas();
			}
			
		},
		error: function(){
			
		}
	});
});

function onClick(event, treeId, treeNode){
	$('#menuForm').form('clear');
	var nodeid=treeNode.id;
	if(nodeid){
		$.ajax({
			url: "/authenticated/ajax/map/select_get_sys_user_group",
			data:JSON.stringify({id:nodeid}),
			success: function(result){
				if(result.success){
					$('#menuForm').form('load',result.data);
				}else{
					$.messager.alert("提示", result.message);
				}
			},
			error: function(){
				$.messager.alert("提示", "请求失败！");
			}
		});
	}
}
/**
 * 初始化form表单组件
 */
function initComboboxDatas(){
	
}

/**
 * 
 */
function addMenu(){
	$('#menuForm').form('clear');
	var selectedNodes = zTreeMenu.getSelectedNodes();
	//var newNode={name:"新建菜单"};
	var parent_id=$("#menuForm :input[name=parent_id]").first();
	//var parent_name=$("#menuForm_parent_name");
	if(selectedNodes.length==0){//添加一级菜单
		parent_id.val(0);
		//parent_name.textbox("setValue","导航菜单");
	}else{
		var selectedNode=selectedNodes[0];
		if(selectedNode.level==0){
			parent_id.val(0);
			//parent_name.textbox("setValue","导航菜单");
		}else{
			parent_id.val(selectedNode.id);
			//parent_name.textbox("setValue",selectedNode.name);
		}
		
	}
}
/**
 * 删除菜单
 */
function deleteMenu(){
	var selectedNodes = zTreeMenu.getSelectedNodes();
	if(selectedNodes.length==0){//添加一级菜单
		$.messager.alert("提示", "请选择删除的菜单！");
	}else{
		var selectedNode=selectedNodes[0];
		if(selectedNode.isParent||selectedNode.level==0){
			$.messager.alert("提示", "父菜单不允许删除！");
			return;
		}
		$.messager.confirm('删除确认', '是否确定删除?', function(r){
			if (r){
				$.ajax({
					type: "post",
					url: "/authenticated/ajax/update/delete_sys_user_group",
					data:JSON.stringify({id:selectedNode.id}),
					dataType:"json",
					success: function(result){
						if(result.success){
							$.messager.alert("提示", "删除成功！<br>");
							initmenu();
						}else{
							$.messager.alert("提示", "删除失败！<br>"+result.message);
						}
					},
					error: function(){
						$.messager.alert("提示", "请求失败！");
					}
				});
			}
		});
		
		
	}
}
function saveMenu(){
	var formObject=$("#menuForm").serializeObject();
	var saveurl="/authenticated/ajax/update/insert_sys_user_group";
	if(formObject.id!=""){
		saveurl="/authenticated/ajax/update/update_sys_user_group";
	}
	$.ajax({
		type: "post",
		url: saveurl,
		data:JSON.stringify(formObject),
		dataType:"json",
		success: function(result){
			if(result.success){
				$.messager.alert("提示", "保存成功！");
				initmenu();
			}else{
				$.messager.alert("提示", "保存失败！");
			}
		},
		error: function(){
			$.messager.alert("提示", "请求失败！");
		}
	});
}

function initmenu(){
	$.ajax({
		type: "post",
		contentType:"application/json",
		async:false,
		url: "/authenticated/ajax/select_sys_user_group",
		data:null,
		dataType:"json",
		success: function(result){
			if(result.success){
				zNodes=result.data;
				zNodes.push(menuRoot);
				zTreeMenu=$.fn.zTree.init($("#menuTree"), ztreeSeting,zNodes);
				zTreeMenu.expandAll(true);
			}
		},
		error: function(){
			
		}
	});
}